package com.samskievert.cactusboom;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.util.Random;


import com.threerings.media.image.BufferedMirage;
import java.awt.image.BufferedImage;

/**
 * Displays a "potential move" cursor to the player.
 */
public class CursorSpriteTerr extends CactusSprite
{
    public boolean didRoll = false;
    public int rollCount = 30;
    private BufferedMirage _image;    
    
    public CursorSpriteTerr ()
    {
        super(new CBObject.Cactus(), new BufferedMirage(new BufferedImage(48,48,BufferedImage.TYPE_3BYTE_BGR)));
        _comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
    }
    
    public void setImage(BufferedMirage image)
    {
        _image = image;
    }

    public void setColor (int color)
    {
        _cactus.owner = color;
    }
    
    public void fixCactus(int rightNum)
    {
        _cactus.fixNumSpikes(rightNum);
    }

    public void setPosition (int x, int y, CBLogic logic, CBObject game) 
    {
        //mouse movement while the room was loading was throwing null pointer exceptions
        if(_cactus != null && game != null && logic != null){
            
            logic.setState(game);
            //logic.setTerritoryState(game);
            
            _cactus.x = x / SIZE;
            _cactus.y = y / SIZE;
            if(game.movePoints != null) numMoves = game.movePoints[_cactus.owner];
            _legal = logic.isLegalMove(_cactus,2);
            moveType = logic.getMoveType(_cactus,2);
            moveDist = logic.isLegalDestination(_cactus) - 1; //we don't count the starting point
            overlap = logic.getOverlap(game.pieces, _cactus.x, _cactus.y);
            
        updateCactus(_cactus);
        }
    }

    public CBObject.Cactus getPiece ()
    {
        return _cactus;
    }
    
    @Override // from CactusSprite
    public void tick(long tickstamp)
    {
     if(didRoll && rollCount > 0)
     {
      rollCount--; 
      invalidate();
     }
    }
    

    @Override // from CactusSprite
    public void paint (Graphics2D gfx)
    {
      if (_legal && didRoll && rollCount == 0) {
          Composite ocomp = gfx.getComposite();
           if(moveType == 3){
               gfx.setComposite(_comp);
               _image.paint(gfx,_bounds.x, _bounds.y);
               gfx.setComposite(ocomp);
               gfx.setColor(Color.black);
               gfx.fillRect(_bounds.x + _bounds.width - 9, _bounds.y + _bounds.height - 9, 8,8);
               Font font =  new Font("arial",0,10);
               gfx.setFont(font);
               gfx.setColor(Color.white);
               gfx.drawString("" + moveDist, _bounds.x + _bounds.width - 7, _bounds.y + _bounds.height - 1);
           } 
      }else if(didRoll && rollCount > 0){
          //insert dice rolling animation here
          Random r = new Random();
          int diceroll = r.nextInt(6) + 1;
          if(rollCount < 2) diceroll = numMoves;
          gfx.setColor(Color.black);
               
           // draw a filled in square
           int px = _bounds.x + 8;
           int py = _bounds.y + 8;
           int pwid = _bounds.width - 16, phei = _bounds.height - 16;
           gfx.fillRect(px, py, pwid, phei);

           Font font =  new Font("arial",0,16);
           gfx.setFont(font);
           gfx.setColor(Color.white);
           gfx.drawString("" + diceroll, _bounds.x + _bounds.width/2 - 4,_bounds.y + _bounds.height/2 + 3);

      }else if(!didRoll){

               gfx.setColor(Color.black);
               
               // draw a filled in square
               int px = _bounds.x + 8;
               int py = _bounds.y + 8;
               int pwid = _bounds.width - 16, phei = _bounds.height - 16;
               gfx.fillRect(px, py, pwid, phei);

               Font font =  new Font("arial",0,10);
               gfx.setFont(font);
               gfx.setColor(Color.white);
               gfx.drawString("Click", _bounds.x + _bounds.width/2 - 12,_bounds.y + _bounds.height/2 - 7);
               gfx.drawString("to", _bounds.x + _bounds.width/2 - 4,_bounds.y + _bounds.height/2 + 3);
               gfx.drawString("Roll", _bounds.x + _bounds.width/2 - 10,_bounds.y + _bounds.height/2 + 13);
               
       }
  }
    
   public int moveDistance = 0;
   public CBObject.Cactus overlap;
   public int moveType;
   public int moveDist;
   public int numMoves;
   public int quadrant;
   protected boolean _legal = false;
   protected AlphaComposite _comp;
}


